<?php
namespace M\Action;
use Common\Model;
use Think\Page;
class PddAction extends BaseAction{
	public function _initialize() {
        parent::_initialize();
        $this->_mod = D('pdditems');
    }
    
    
public function index(){
$useragent = strtolower(addslashes($_SERVER['HTTP_USER_AGENT'])); 
if(strpos($useragent, 'micromessenger') > 1 && strpos($useragent, 'android')>1){ 
$this->assign('isweixin',true);
}
$size	= 10;
$cid		= I('cid','','trim');
$sort	= I('sort', 'new', 'trim');
$this->assign('txt_sort', $sort);
$this->assign('cid', $cid);
$key    = trimall(I("k",'','htmlspecialchars'));
$key    = urldecode($key);
if($key){
 $where['goods_name'] = array( 'like', '%' . $key . '%' );
 $this->assign('sokey', $key);
}

if($cid){
 $where['category_id'] = $cid;
}
$count =$this->_mod->where($where)->count();
$this -> assign('page_size',ceil($count/$size));

if($cid){
$cateinfo='拼多多'.pdd_cate($cid);
}
if($key){
 $this->_config_seo(array(
            'cate_name' => $cateinfo,
            'title' => '搜索"' . $key . '"的优惠券结果页 - '. C('yh_site_name'),
            'seo_keywords' => $cateinfo['seo_keys'],
            'seo_description' => $cateinfo['seo_desc']
   ));
	
 }else{
 $this->_config_seo(C('yh_seo_config.cate'), array(
            'cate_name' => $cateinfo?$cateinfo:'拼多多'
   ));
  }
 
$this->display();

}


public function catelist(){
$page	= I('p',0,'trim');
$size	= 10;
$cid		= I('cid','','trim');
$sort	= I('sort', 'new', 'trim');
$start = $size * ($page-1);
$this->assign('txt_sort', $sort);
$this->assign('cid', $cid);
$key    = trimall(I("k",'','htmlspecialchars'));
$key    = urldecode($key);
if($key){
 $where['goods_name'] = array( 'like', '%' . $key . '%' );
}

if($cid){
 $where['category_id'] = $cid;
}
$order = 'tuisong desc,orderid asc';
switch ($sort){
    		case 'new':
				$order.= ', addtime DESC';
				break;
			case 'price':
				$order.= ', min_group_price asc';
				break;
			case 'rate':
				$order.= ', coupon_discount asc';
				break;
			case 'hot':
				$order.= ', sold_quantity DESC';
				break;
			case 'default':
				$order.= ', id DESC';
}

$items_list = $this->_mod->where($where)->field('min_normal_price,goods_id,promotion_rate,goods_name,goods_thumbnail_url,sold_quantity,min_group_price,coupon_discount,addtime')->order($order)->limit($start . ',' . $size)->select();

$count =$this->_mod->where($where)->count();
$pager = new Page($count, $size);
$this->assign('p', $page);
$this->assign('page', $pager->show());
$this->assign('total_item', $count);
$this -> assign('page_size',$size);
if($items_list){
$today=date('Ymd');
$goodslist=array();
foreach($items_list as $k=>$v){
$goodslist[$k]['id']=$v['goods_id'];
$goodslist[$k]['title']=$v['goods_name'];
$goodslist[$k]['pic_url']=$v['goods_thumbnail_url'];
$goodslist[$k]['coupon_price']=$v['min_group_price']-$v['coupon_discount'];
$goodslist[$k]['price']=$v['min_normal_price'];
$goodslist[$k]['group_price']=$v['min_group_price'];
$goodslist[$k]['promotion_rate']=$v['promotion_rate'];
$goodslist[$k]['quan']=$v['coupon_discount'];
$goodslist[$k]['volume']=$v['sold_quantity'];	
if($today==date('Ymd',$v['addtime'])){
$goodslist[$k]['is_new']=1;	
}else{
$goodslist[$k]['is_new']=0;		
}
if(C('APP_SUB_DOMAIN_DEPLOY')){
$goodslist[$k]['linkurl']=U('/pdditem/',array('id'=>$v['goods_id']));
}else{
$goodslist[$k]['linkurl']=U('pdditem/index',array('id'=>$v['goods_id']));
}
	
}
}

$this->assign('list',$goodslist);
	
	
$this->display();	
}








}